package com.ruoyi.dl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dl.domain.DlRecreation;
import com.ruoyi.dl.service.IDlRecreationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 游戏管理Controller
 *
 * @author wangshouli
 * @date 2021-01-04
 */
@Controller
@RequestMapping("/dl/recreation")
public class DlRecreationController extends BaseController {
    private String prefix = "dl/recreation";

    @Autowired
    private IDlRecreationService dlRecreationService;

    @RequiresPermissions("dl:recreation:view")
    @GetMapping()
    public String recreation() {
        return prefix + "/recreation";
    }

    /**
     * 查询游戏管理列表
     */
    @RequiresPermissions("dl:recreation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DlRecreation dlRecreation) {
        startPage();
        List<DlRecreation> list = dlRecreationService.selectDlRecreationList(dlRecreation);
        return getDataTable(list);
    }

    /**
     * 导出游戏管理列表
     */
    @RequiresPermissions("dl:recreation:export")
    @Log(title = "游戏管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DlRecreation dlRecreation) {
        List<DlRecreation> list = dlRecreationService.selectDlRecreationList(dlRecreation);
        ExcelUtil<DlRecreation> util = new ExcelUtil<DlRecreation>(DlRecreation.class);
        return util.exportExcel(list, "recreation");
    }

    /**
     * 新增游戏管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存游戏管理
     */
    @RequiresPermissions("dl:recreation:add")
    @Log(title = "游戏管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DlRecreation dlRecreation) {
        return toAjax(dlRecreationService.insertDlRecreation(dlRecreation));
    }

    /**
     * 修改游戏管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        DlRecreation dlRecreation = dlRecreationService.selectDlRecreationById(id);
        mmap.put("dlRecreation", dlRecreation);
        return prefix + "/edit";
    }

    /**
     * 修改保存游戏管理
     */
    @RequiresPermissions("dl:recreation:edit")
    @Log(title = "游戏管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DlRecreation dlRecreation) {
        return toAjax(dlRecreationService.updateDlRecreation(dlRecreation));
    }

    /**
     * 删除游戏管理
     */
    @RequiresPermissions("dl:recreation:remove")
    @Log(title = "游戏管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dlRecreationService.deleteDlRecreationByIds(ids));
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
    public Map<String, String> upload(@RequestParam("file") MultipartFile file) {
        Map<String, String> map = new HashMap<>();
        try {
            String avatar = FileUploadUtils.upload(file);
            map.put("data", avatar);
            map.put("code", "0");
        } catch (Exception e) {
            map.put("data", e.getMessage().toString());
            map.put("code", "0");
        }
        return map;
    }
}
